package com.wpzf.ndhc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wpzf.ndhc.utils.TableUtil;
import com.wpzf.ndhc.mapper.WpzfNdhcAppMapper;
import com.wpzf.ndhc.service.WpzfNdhcAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WpzfNdhcAppServiceImpl implements WpzfNdhcAppService {

    @Autowired
    WpzfNdhcAppMapper wpzfNdhcAppMapper;


    @Override
    public List<Map<String, Object>> queryPch(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryPch(parm);
    }

    @Override
    public List<Map<String, Object>> queryXzqdmList(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryXzqdmList(parm);
    }

    @Override
    public Integer queryXzqdmListCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryXzqdmListCount(parm);
    }

    @Override
    public List<Map<String, Object>> queryDkTaskList(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryDkTaskList(parm);
    }

    @Override
    public Integer queryDkTaskListCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryDkTaskListCount(parm);
    }

    @Override
    public List<Map<String, Object>> queryProjectList(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryProjectList(parm);
    }

    @Override
    public Integer queryProjectListCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryProjectListCount(parm);
    }

    @Override
    public List<Map<String, Object>> queryTaskList(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTaskList(parm);
    }

    @Override
    public Integer queryTaskListCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTaskListCount(parm);
    }

    @Override
    public List<Map<String, Object>> queryTbList(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTbList(parm);
    }

    @Override
    public Integer queryTbListCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTbListCount(parm);
    }

    @Override
    public Map<String, Object> queryTbInfo(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTbInfo(parm);
    }

    @Override
    public List<Map<String, Object>> queryWKT(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryWKT(parm);
    }

    @Override
    public Map<String, Object> getTaskDetailAnnex(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.getTaskDetailAnnex(parm);
    }

    @Override
    public Integer saveTaskDetailAnnex(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        Integer rest = 0;
        Map<String, Object> map = getTaskDetailAnnex(parm);
        if (map != null) {
            parm.put("uuid", parm.get("uuid"));
            parm.put("modifier", parm.get("username"));
            parm.put("modifydate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
            rest = wpzfNdhcAppMapper.updateTaskDetailAnnex(parm);
        } else {
            parm.put("creator", parm.get("username"));
            parm.put("createdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
            String uuid = UUID.randomUUID().toString();
            parm.put("uuid", uuid);
            rest = wpzfNdhcAppMapper.saveTaskDetailAnnex(parm);
        }
        return rest;
    }

    @Override
    public Integer updateFjInfo(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.updateFjInfo(parm);
    }

    @Override
    public Integer save(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        parm = (Map<String, Object>) parm.get("mainData");
        return wpzfNdhcAppMapper.save(parm);
    }

    @Override
    public Integer delete(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.delete(parm);
    }

    @Override
    public Map<String, Object> queryTbxxbxj(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryTbxxbxj(parm);
    }

    @Override
    public List<Map<String, Object>> queryReTaskdetailAnnex(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.queryReTaskdetailAnnex(parm);
    }


    public String getInfo(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);

        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> main = new HashMap<>();
        Map<String, Object> wpzfdk = new HashMap<>();
        Map<String, Object> tbxxbxj = queryTbxxbxj(parm);
        wpzfdk.put("tbbsm", "");
        wpzfdk.put("tbbh", "");
        wpzfdk.put("dkbh", "");
        wpzfdk.put("sjtbmj", "");
        wpzfdk.put("jcrq", "");
        wpzfdk.put("xzqdm", "");
        wpzfdk.put("tbfl", "");
        wpzfdk.put("xzmc", "");
        wpzfdk.put("csmc", "");
        wpzfdk.put("xxdz", "");
        wpzfdk.put("geometry", tbxxbxj.get("wkt"));
        wpzfdk.put("dlrdnydmj", "");
        wpzfdk.put("dlrdgdmj", "");
        wpzfdk.put("yjjbntmj", "");
        wpzfdk.put("wsx_dlrdstmj", "");
        wpzfdk.put("stbhhxmj", "");
        wpzfdk.put("hcjl", "");
        wpzfdk.put("tblx", "");
        wpzfdk.put("sjyt", "");
        wpzfdk.put("sjyt2", "");
        wpzfdk.put("hfsjyt", "");
        wpzfdk.put("hfpdyjsm", "");
        wpzfdk.put("nydmj", "");
        wpzfdk.put("gdmj", "");
        wpzfdk.put("stmj", "");
        wpzfdk.put("sjdkmj", "");
        wpzfdk.put("tb_stbhhxmj", "");
        wpzfdk.put("wflx1", "");
        wpzfdk.put("wflx2", "");
        wpzfdk.put("wflx3", "");
        wpzfdk.put("wflx4", "");
        wpzfdk.put("lzgdjf", "");
        wpzfdk.put("sywhzj", "");
        wpzfdk.put("sfdpf", "");
        wpzfdk.put("wfsjyt", "");
        wpzfdk.put("wfpdyjsm", "");
        wpzfdk.put("sfjsyd", "");
        wpzfdk.put("tbjsqk", "");
        wpzfdk.put("wf_xmmc", "");
        wpzfdk.put("wf_xmzt", "");
        wpzfdk.put("wf_xmlx", "");
        wpzfdk.put("wf_lxpzjg", "");
        wpzfdk.put("wf_lxpzwh", "");
        wpzfdk.put("wf_clfs", "");
        wpzfdk.put("sfylwsss", "");
        wpzfdk.put("sfbzxajzf", "");
        wpzfdk.put("sfjjxm", "");
        wpzfdk.put("wf_labh", "");
        wpzfdk.put("yjbm", "");
        wpzfdk.put("zglsdwqk", "");
        wpzfdk.put("fgymmj", "");
        wpzfdk.put("fgdwmj", "");
        wpzfdk.put("ccfg_yjjbnt", "");
        wpzfdk.put("ccfg_stbhq", "");
        wpzfdk.put("fgysdw", "");
        wpzfdk.put("fgysfzr", "");
        wpzfdk.put("qtydsjyt", "");
        wpzfdk.put("qtydsjyt2", "");
        wpzfdk.put("pwsj", "");
        wpzfdk.put("qtpdyjsm", "");

        Map<String, Object> other = new HashMap<>();
        List<Map<String, Object>> hcfjList = new ArrayList<>();
        List<Map<String, Object>> reTaskdetailAnnexList = queryReTaskdetailAnnex(parm);
        for (Map<String, Object> reTaskdetailAnnex : reTaskdetailAnnexList) {
            String fj = reTaskdetailAnnex.get("fj") + "";
            if (fj.indexOf("jpg")!=-1) {
                //todo
                Map<String, Object> hcfj = new HashMap<>();
                hcfj.put("bsm", "");
                hcfj.put("xzqdm", "");
                hcfj.put("tbbsm", "");
                hcfj.put("tbbh", "");
                hcfj.put("fjmc", reTaskdetailAnnex.get("fjmc"));
                hcfj.put("lysb", "");
//                hcfj.put("fjlx", "IMAGE");
                hcfj.put("fjlx", reTaskdetailAnnex.get("fjlx"));
                hcfj.put("psry", reTaskdetailAnnex.get("psry"));
                hcfj.put("pssj", reTaskdetailAnnex.get("pssj"));
                hcfj.put("longitude", reTaskdetailAnnex.get("longitude"));
                hcfj.put("latitude", reTaskdetailAnnex.get("latitude"));
                hcfj.put("psjd", reTaskdetailAnnex.get("psjd"));
                hcfj.put("psgd", reTaskdetailAnnex.get("psgd"));
                hcfj.put("psfyj", reTaskdetailAnnex.get("psfyj"));
                hcfj.put("psaltitude", reTaskdetailAnnex.get("psaltitude"));
                hcfj.put("takeofflon", "");
                hcfj.put("takeofflat", "");
                hcfj.put("takeoffrelheight", "");
                hcfj.put("takeoffaltitude", "");
                hcfj.put("metarecord", "");
                hcfj.put("arg", "");
                hcfj.put("url", reTaskdetailAnnex.get("fj"));

                hcfj.put("creator", reTaskdetailAnnex.get("creator"));
                hcfj.put("xzb", reTaskdetailAnnex.get("xzb"));
                hcfj.put("yzb", reTaskdetailAnnex.get("yzb"));
                hcfj.put("ower_lever", reTaskdetailAnnex.get("ower_lever"));
                hcfj.put("uuid", reTaskdetailAnnex.get("uuid"));
                hcfj.put("dkbh", reTaskdetailAnnex.get("dkbh"));

                hcfjList.add(hcfj);
            }
        }


        other.put("pwinfo", new ArrayList<>());
        other.put("fjinfo", new ArrayList<>());
        other.put("hcfj", hcfjList);
        data.put("other", other);
        main.put("wpzfdk", wpzfdk);
        data.put("main", main);
        dataList.add(data);
        map.put("data", dataList);
        JSONObject jsonObject = new JSONObject(map);
        System.out.println(jsonObject.toJSONString());
        return jsonObject.toJSONString();
    }

    @Override
    public List<Map<String, Object>> getFlieInfo(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcAppMapper.getFlieInfo(parm);
    }


}
